<?php

namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

use think\Db;

/**
 * 图表
 */
class Analysis extends Backend
{
    public function __construct()
    {
        parent::__construct();

        $this->CateModel = model('Post.Cate');
        $this->PostModel = model('Post.Post');
        $this->UserModel = model('User.User');
        $this->RecordModel = Model('User.Record');
    }

    public function post()
    {
        $catelist = $this->CateModel->field('id,name')->order('id DESC')->select();

        //array_column()抽取出二维数组中指定字段的内容变成一个新数组
        $row = array_column($catelist, 'name');

        foreach ($catelist as $item) {

            // 已结
            $where = [
                'accept' => ['exp', Db::raw("is not null")],
                'cateid' => $item['id']
            ];
            $AcceptList[] =  $this->PostModel->where($where)->count();

            // 未结
            $whres = [
                'accept' => ['exp', Db::raw("is null")],
                'cateid' => $item['id']
            ];
            $NotAcceptList[] = $this->PostModel->where($whres)->count();
        }
        $this->assign([
            //转换为json
            'catelist' => json_encode($row),
            'AcceptList' => json_encode($AcceptList),
            'NotAcceptList' => json_encode($NotAcceptList),
        ]);

        return $this->fetch();
    }

    // 用户量统计
    public function user()
    {
        $catelist = $this->UserModel->field('vip,count(vip) as v')->group('vip')->select();

        //array_column()抽取出二维数组中指定字段的内容变成一个新数组
        $row = array_column($catelist, 'vip', 'v');
        foreach ($row as $key => $item) {
            $row[$key] = 'vip' . $item;
        }
        $this->assign([
            //转换为json
            'catelist' => json_encode($row)
        ]);
        return $this->fetch();
    }
    // 充值统计
    public function recharge()
    {
        $catelist = $this->RecordModel->where(['state' => 3])->select();

        //array_column()抽取出二维数组中指定字段的内容变成一个新数组
        $row = array_column($catelist, 'createtime');
        foreach ($row as $key => $item) {
            $row[$key] = date("Y-m-d", strtotime($item));
        }
        $this->assign([
            //转换为json
            'catelist' => json_encode(array_count_values($row))
        ]);
        return $this->fetch();
    }
}
